package com.xch.sort;

import cn.hutool.core.comparator.CompareUtil;

public class SortingHelper {

    public static <E extends Comparable> boolean isSorted(E[] arr) {
        for (int i = 1; i < arr.length; i++)
            if (CompareUtil.compare(arr[i - 1], arr[i]) > 0) return false;

        return true;
    }


    public static <E extends Comparable> void sort(String sortName, E[] arr) {


        long startTime = System.nanoTime();
        if (sortName.equals("SelectionSort")) SelectionSort.sort(arr);
        else if (sortName.equals("InsertionSort")) InsertionSort.sort(arr);

        double endTime = System.nanoTime();

        if (!SortingHelper.isSorted(arr)) {
            throw new RuntimeException(sortName + "   failed");
        }
        double v = (endTime - startTime) / 1000000000;
        System.out.println(v + "s");
        System.out.println(String.format("%s n=%d :%f s",sortName,arr.length,v));
    }

}
